<template>
  <el-dialog title="新增员工" :visible="showDialog" @close="closeDialog">
    <!-- 表单 -->
    <el-form ref="addForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in hireTypeList" :key="item.id" :value="item.id" :label="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="inputFocus" />
        <div v-if="threeFlag" class="tree-box">
          <el-tree :data="threeLists" :props="{ label: 'name' }" @node-click="clickNode" />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="$emit('update:show-dialog', false)">取消</el-button>
      <el-button type="primary" @click="addDepartment">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 枚举静态数据
import EmployeeEnum from '@/api/constant/employees'
// 员工相关接口
import { addEmployee } from '@/api/employees'
// 部门相关接口
import { getDepartments } from '@/api/departments'
// 平面数组转树形数组
import { treeDiagramList } from '@/utils/index'
export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      hireTypeList: EmployeeEnum.hireType, // 静态枚举，聘用形式
      threeLists: [], // 树形列表
      threeFlag: false, // 控制树形图是否显示
      originalData: [], // 部门所有的原始数据
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  mounted() {
    console.log(this.$parent)
    this.HGetDepartments()
  },
  methods: {
    // 子组件点击关闭对话框
    closeDialog() {
      this.$emit('update:show-dialog', false)
    },
    // 获取所有部门
    async HGetDepartments() {
      const res = await getDepartments()
      this.originalData = res.depts // 原始数据
      this.threeLists = treeDiagramList(res.depts) // 处理成树形数组
    },
    // 部门表单获得焦点是触发,控制树形结构显示
    inputFocus() {
      this.threeFlag = true
      console.log(this.originalData)
    },
    // 点击树形结构
    clickNode(data) {
      // 忽略有子数组的
      if (data.children && data.children.length > 0) return
      this.formData.departmentName = data.name
      // 点击关闭树形
      this.threeFlag = false
    },
    async addDepartment() {
      await addEmployee(this.formData)
      this.$message.success('添加员工成功!')
      // 通知父组件关闭对话框，并且通过 $parent调用父组件的方法，重新拉取下数据
      this.$emit('update:show-dialog', false)
      this.$parent.hReqGetEmployeeList()
    }
  }
}
</script>
